﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.pro2e.web.util;
using com.pro2e.console.service.log.vo;
using sexhealth.console.service.post;
using sexhealth.console.service.activity.vo;
using System.IO;
using com.pro2e.console.core.util;
using NHibernate.Criterion;

public partial class activityAdmin_UC01_Export01 : System.Web.UI.Page
{
    ActivityService activityService = new ActivityService();
    SessionHelper sHelper = new SessionHelper();

    protected void Page_Load(object sender, EventArgs e)
    {
        ((activityAdmin_AdmMasterPage)Master).setPrgName("徵文報表匯出");

        if (!IsPostBack)
        {
            
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        DateTime startDate = datePicker.DateStart;
        DateTime endDate = ConvertUtil.ToDateTimeMax(datePicker.DateEnd);

        lblTitle.Text = string.Format("期間:{0}年{1}月{2}日起至{3}年{4}月{5}日"
            , startDate.Year, startDate.Month, startDate.Day
            , endDate.Year, endDate.Month, endDate.Day);

        DetachedCriteria dCriteria = DetachedCriteria.For(typeof(AnswerVO));

        dCriteria.Add(Expression.Eq("AnswerType", (int)AnswerType.SignUp));
        dCriteria.Add(Expression.Eq("ActivityId", AnswerVO.LatestActivityId));

        if (startDate != null)
        {
            dCriteria.Add(Expression.Ge("CreatedDate", startDate));
        }
        if (endDate != null)
        {
            dCriteria.Add(Expression.Le("CreatedDate", endDate));
        }

        gvList.DataSource = activityService.ExecutableDetachedCriteria<AnswerVO>(dCriteria);
        gvList.DataBind();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        lblTitle.Visible = true;
        string attachment = "attachment; filename=Signup.xls";
        Response.Charset = "big5";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        pnlExport.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        lblTitle.Visible = false;
    }
}